package com.example.yk2021xm04.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yk2021xm04.mapper.NoticeMapper;
import com.example.yk2021xm04.pojo.Notice;
import com.example.yk2021xm04.pojo.User;
import com.example.yk2021xm04.query.NoticeQuery;
import com.example.yk2021xm04.service.INoticeService;
import com.example.yk2021xm04.utils.AssertUtil;
import com.example.yk2021xm04.utils.PageResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiYongle
 * @since 2021-11-30
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {
    @Override
    public void addNotice(Notice notice) {
        /**
         * 通知内容
         * 非空
         */
        AssertUtil.isTrue(StringUtils.isBlank(notice.getNoticeContent()),"通知内容不能为空");
        notice.setNoticeSendtime(LocalDateTime.now());
        AssertUtil.isTrue(!(this.save(notice)),"通知记录添加失败");
    }

    @Override
    public Map<String, Object> noticeList(NoticeQuery noticeQuery) {
        //分页的类
        IPage<Notice> page = new Page<Notice>(noticeQuery.getPage(), noticeQuery.getLimit());
        //QueryWrapper：sql条件查询
        //eq("is_del", 0) 代表 where is_del=0
        //详细可搜索相关用法
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>();
//        queryWrapper.eq("is_del",0).eq("active_status",1);

        //这里就是代表 sql语句中的 like
        if(StringUtils.isNotBlank(noticeQuery.getNoticeContent())) {
            queryWrapper.like("notice_content",noticeQuery.getNoticeContent());
        }
        //已经写好的接口
        page = this.baseMapper.selectPage(page,queryWrapper);
        System.out.println(page.getTotal());
        return PageResultUtil.getResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void deleteNotice(Integer[] ids) {
        /**
         * 批量删除资产
         */
        AssertUtil.isTrue(null==ids || ids.length==0,"请选择待删除的id");
        AssertUtil.isTrue(!(this.removeByIds(Arrays.asList(ids))),"通知记录删除失败");
    }

    @Override
    public void updateNotice(Notice notice) {
        AssertUtil.isTrue(StringUtils.isBlank(notice.getNoticeContent()),"通知内容不能为空");
        Notice temp = this.findNoticeByNoticeContent(notice.getNoticeContent());
        AssertUtil.isTrue(!(this.updateById(notice)),"通知内容更新失败");
    }

    private Notice findNoticeByNoticeContent(String noticeContent) {
        return this.baseMapper.selectOne(new QueryWrapper<Notice>().eq("notice_content",noticeContent));
    }

    @Override
    public List<Object> anTimeByDay() {return this.baseMapper.anTimeByDay();}

    @Override
    public List<Object> anCountByDay() {return this.baseMapper.anCountByDay();}

    @Override
    public Integer NoticeCountInDay() {return this.baseMapper.NoticeCountInDay();}

    @Override
    public Integer NoticeCountInYesterday() {return this.baseMapper.NoticeCountInYesterday();}
}
